package com.hd.trans.framework.dialog;


import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.hd.trans.R;
import com.hd.trans.framework.component.DlgCancelCallback;


/**
 * 
 * @ClassName: LoadingDialog
 * @Description: 加载框
 * @author yangHuang
 * @date 2016-3-9
 */
public class LoadingDialog extends Dialog {

	private TextView tvLoading;
	private TextView tvCancel;
	private DlgCancelCallback cancelCallback;

	public LoadingDialog(Context context) {
		super(context, R.style.LoadingDialog);
		init(context);
	}

	public LoadingDialog(Context context, int defStyle) {
		super(context, defStyle);
		init(context);
	}

	protected LoadingDialog(Context context, boolean cancelable,
                            OnCancelListener listener) {
		super(context, cancelable, listener);
		init(context);
	}
	
	private void init(Context context) {
		setCanceledOnTouchOutside(true);
		setCancelable(false);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		View view = LayoutInflater.from(context).inflate(
				R.layout.dialog_loading, null);
		tvLoading = (TextView) view.findViewById(R.id.loading_msg);
		tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
		tvCancel.setOnClickListener(v -> {
			if(cancelCallback != null){
				dismiss();
				cancelCallback.onCancel();
			}
		});
		setContentView(view);
	}
	

	public void setLoadMsg(String content){
		tvLoading.setText(content);
	}

	public void hideCancelButton(){
		tvCancel.setVisibility(View.GONE);
	}

	public void setCancelCallback(DlgCancelCallback cancelCallback) {
		this.cancelCallback = cancelCallback;
		if(cancelCallback != null){
			tvCancel.postDelayed(() -> {
				if (isShowing()) {
					tvCancel.setVisibility(View.VISIBLE);
				}
			}, 5000);
		}
	}
}
